//
//  BIDViewController.m
//  SlowWorker
//
//  Created by Robin Gao on 8/7/12.
//  Copyright (c) 2012 Robin Gao. All rights reserved.
//

#import "BIDViewController.h"

@interface BIDViewController ()

@end

@implementation BIDViewController

@synthesize startButton, resultTextView;


-(NSString *)fetchSomethingFromServer
{
    [NSThread sleepForTimeInterval:1];
    return @"Hi there";
}

-(NSString *)processData: (NSString *)data
{
    [NSThread sleepForTimeInterval:2];
    return [data uppercaseString];
}

-(NSString *)calculateFirstResult:(NSString *)data
{
    [NSThread sleepForTimeInterval:3];
    return [NSString stringWithFormat:@"Number of chars: %d", [data length]];
}

-(NSString *)calculateSecondResult:(NSString *)data
{
    [NSThread sleepForTimeInterval:4];
    return [data stringByReplacingOccurrencesOfString:@"E" withString:@"e"];
}

-(IBAction)doWork:(id)sender
{
    NSDate *startTime = [NSDate date];
    NSString *fetchedData =[self fetchSomethingFromServer];
    NSString *processedData =[self processData:fetchedData];
    NSString *firstResult = [self calculateFirstResult:processedData];
    NSString *secondReslut = [self calculateSecondResult:processedData];
    
    NSString *resultsSummary =[NSString stringWithFormat:
                               @"First: [%@]\nSecond: [%@]",firstResult, secondReslut];
    
    resultTextView.text = resultsSummary;
    NSDate *endTime = [NSDate date];
    NSLog(@"Completed in %f seconds", [endTime timeIntervalSinceDate:startTime]);
        
}



- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    
    self.startButton = nil;
    self.resultTextView = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end
